#include <gtest/gtest.h>
#include <gflags/gflags.h>

#include "channel.hpp"
#include "etcd.hpp"     //服务注册
#include <thread>
#include <vector>
#include "utils.hpp"

#include "transmite.pb.h"
#include "base.pb.h"
#include "user.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式, fasle-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "这是服务中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "服务监控目录");

xchat::ServiceManager::ptr _user_channels;
auto service_manager = std::make_shared<xchat::ServiceManager>();


void string_message(const std::string &uid, const std::string &ssid, const std::string &msg)
{
    auto channel = service_manager->choose(FLAGS_transmite_service);
    ASSERT_TRUE(channel != nullptr);
    xchat::MsgTransmitService_Stub stub(channel.get());
    xchat::NewMessageReq req;
    
    req.set_request_id(xchat::uuid());
    req.set_user_id(uid);
    //req.set_session_id("这一栏没用");
    req.set_chat_session_id(ssid);
    req.mutable_message()->set_message_type(xchat::MessageType::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);

    std::unique_ptr<xchat::GetTransmitTargetRsp> rsp(new xchat::GetTransmitTargetRsp());
    std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
    stub.GetTransmitTarget(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
}

void image_message(const std::string &uid, const std::string &ssid, const std::string &msg)
{
    auto channel = service_manager->choose(FLAGS_transmite_service);
    ASSERT_TRUE(channel != nullptr);
    xchat::MsgTransmitService_Stub stub(channel.get());
    xchat::NewMessageReq req;
    
    req.set_request_id(xchat::uuid());
    req.set_user_id(uid);
    //req.set_session_id("这一栏没用");
    req.set_chat_session_id(ssid);
    req.mutable_message()->set_message_type(xchat::MessageType::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);

    std::unique_ptr<xchat::GetTransmitTargetRsp> rsp(new xchat::GetTransmitTargetRsp());
    std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
    stub.GetTransmitTarget(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
}

void file_message(const std::string &uid, const std::string &ssid, 
        const std::string &file_name, const std::string &content)
{
    auto channel = service_manager->choose(FLAGS_transmite_service);
    ASSERT_TRUE(channel != nullptr);
    xchat::MsgTransmitService_Stub stub(channel.get());
    xchat::NewMessageReq req;
    
    req.set_request_id(xchat::uuid());
    req.set_user_id(uid);
    //req.set_session_id("这一栏没用");
    req.set_chat_session_id(ssid);
    req.mutable_message()->set_message_type(xchat::MessageType::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(file_name);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());

    std::unique_ptr<xchat::GetTransmitTargetRsp> rsp(new xchat::GetTransmitTargetRsp());
    std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
    stub.GetTransmitTarget(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
}

void speech_message(const std::string &uid, const std::string &ssid, const std::string &msg)
{
    auto channel = service_manager->choose(FLAGS_transmite_service);
    ASSERT_TRUE(channel != nullptr);
    xchat::MsgTransmitService_Stub stub(channel.get());
    xchat::NewMessageReq req;
    
    req.set_request_id(xchat::uuid());
    req.set_user_id(uid);
    //req.set_session_id("这一栏没用");
    req.set_chat_session_id(ssid);
    req.mutable_message()->set_message_type(xchat::MessageType::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);

    std::unique_ptr<xchat::GetTransmitTargetRsp> rsp(new xchat::GetTransmitTargetRsp());
    std::unique_ptr<brpc::Controller> cntl(new brpc::Controller());
    stub.GetTransmitTarget(cntl.get(), &req, rsp.get(), nullptr); // 同步调用
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(rsp->success());
}

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    
    xchat::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);


    //1.先构造Rpc信道管理对象
    service_manager->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&xchat::ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&xchat::ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    //2.构造服务发现对象
    xchat::Discovery::ptr dclient = std::make_shared<xchat::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    //21df-12664a1c-0000 张三三三 f7aa-b126b9c7-0001张三
    // string_message("21df-12664a1c-0000", "会话ID1", "吃饭了吗?");
    // string_message("f7aa-b126b9c7-0001", "会话ID1", "吃的盖浇饭!");

    // image_message("21df-12664a1c-0000", "会话ID1", "张三三三image_content");
    // speech_message("21df-12664a1c-0000", "会话ID1", "张三三三speech_content");
    // file_message("f7aa-b126b9c7-0001", "bf19-c0f652cf-0000", "张三联调文件的名称", "张三联调文件内容");
    // file_message("f7aa-b126b9c7-0001", "bf19-c0f652cf-0000", "张三联调文件的名称2", "张三联调文件内容2");
    // speech_message("9dbd-b84377c2-0002", "b287-4a734db1-0000", "赵六文件");
    //file_message("9dbd-b84377c2-0002", "b287-4a734db1-0000", "赵六文件", "11");
    image_message("9dbd-b84377c2-0002", "b287-4a734db1-0000", "赵六图片图片图片图片图片图片图片图片图片图片图片图片图片图片图片图片图片文件");
    image_message("9dbd-b84377c2-0002", "b287-4a734db1-0000", "赵六");


    return 0;
}